﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ReallyReallyReallySimpleRoguelike.Helpers
{
    /// <summary>
    /// Contains the code for displaying text within the game panels.
    /// </summary>
    public class Panel
    {
        /// <summary>
        /// 
        /// </summary>
        private Point firstCharacter;
        /// <summary>
        /// 
        /// </summary>
        private int lengthInCharacters;

        private string currentString;
        private ConsoleColor currentColor;

        /// <summary>
        /// Creates an instance of panel
        /// </summary>
        /// <param name="point"></param>
        /// <param name="length"></param>
        public Panel(Point point,int length)
        {
            currentString = "";
            currentColor = ConsoleColor.White;
            firstCharacter = point;
            lengthInCharacters = length;
        }

        public void Clear() 
        { 
            Write (" ",ConsoleColor.White);
        }

        public void Write(string text, ConsoleColor color) 
        {
            if (currentColor != color || currentString != text)
            {
                currentColor = color;
                currentString = text;

                Console.ForegroundColor = color;
                Console.SetCursorPosition(firstCharacter.X, firstCharacter.Y);

                if (text.Length < lengthInCharacters)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(' ', (lengthInCharacters - text.Length) / 2);
                    sb.Append(text);
                    sb.Append(' ', (lengthInCharacters - text.Length) / 2);

                    text = sb.ToString();
                }

                if (text.Length > lengthInCharacters)
                    text = text.Remove(lengthInCharacters);

                Console.WriteLine(text);
            }
        }
    }
}
